﻿

using Furion.DependencyInjection;
using Furion.DynamicApiController;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IotContract.Models.Clients;
using IotContract.Models.Process;
using Microsoft.AspNetCore.Mvc;
using IotFun.Services;
using IotFun.Const;
using IotContract.Models.Mqtt;
using Admin.NET.Core;
using IotFun.Views.EquipTagView.Dto;
using IotFun.Views.MqttView.Dto;
using IotContract.Interfaces;
using IotContract.Models;
using ProtoBuf.Grpc.Configuration;
using Furion.FriendlyException;
using IotFun.Entity;
using IotFun.Views.EquipInfoView.Dto;

namespace IotFun.Views.MqttView;

[ApiDescriptionSettings(IotConst.GroupName)]
public class MqttService : IDynamicApiController, ITransient
{
    private GrpcService _grpcService;
  
    public MqttService(GrpcService grpcService)
    {
        _grpcService = grpcService;
}
    [HttpGet]
    [ApiDescriptionSettings(Name = "ClientPage")]
    public async Task<SqlSugarPagedList<MqttClientInfo>> Page([FromQuery] MqttClientInfoInput input)
    {
        var serverStarted = _grpcService.GetMqttServerStatus().Result.Started;
        if(!serverStarted)
        {
            throw Oops.Oh("Mqtt服务器未启动，启动后再查询");
        }
        List<MqttClientInfo> list = _grpcService.GetMqttClientInfos(new MqttProviderCommand()).Result;
        SqlSugarPagedList<MqttClientInfo> sqlSugarPagedList = new SqlSugarPagedList<MqttClientInfo>();
        if (list != null)
        {
            sqlSugarPagedList.Total = list.Count;
            sqlSugarPagedList.Items = list;
        }
        sqlSugarPagedList.TotalPages = 1;
        sqlSugarPagedList.Page = 1;
        sqlSugarPagedList.HasNextPage = false;
        sqlSugarPagedList.HasPrevPage = false;
        sqlSugarPagedList.PageSize = 100;

        return sqlSugarPagedList;
    }

    [HttpGet]
    [ApiDescriptionSettings(Name = "Start")]
    public async Task<MqttGrpcReturn> MqttServerStart([FromQuery] int interval)
    {
        //MQTT启动
        if (interval <= 0)
        {
            interval = 1000;
        }
        var res = await _grpcService.StartOrStopMqttServer(new MqttProviderCommand() { Start = true,Interval = interval });

        return res;
    }

    [HttpGet]
    [ApiDescriptionSettings(Name = "Stop")]
    public async Task<MqttGrpcReturn> MqttServerStop()
    {
        //MQTT停止
        var res =  await _grpcService.StartOrStopMqttServer(new MqttProviderCommand() { Start = false});

        return res;
    }
    [HttpGet]
    [ApiDescriptionSettings(Name = "MqttServerStatus")]
    public async Task<MqttStatusReturn> MqttServerStatus()
    {
        var mqttStatus = _grpcService.GetMqttServerStatus().Result;
        return new MqttStatusReturn
        {
            Started = mqttStatus.Started,
            Interval = mqttStatus.Interval
        };
    }

    [HttpGet]
    [ApiDescriptionSettings(Name = "updateDataPublish")]
    public async Task<MqttGrpcReturn> MqttDataChangePublish([FromQuery] MqttDataPublishStatus input)
    {
        //MQTT ProcessData启动发布
        var res = await _grpcService.StartOrStopMqttProcessData(new MqttProviderCommand() 
        {
            EquipType = input.DataType,
            Start = input.IsPublish == 1,
            Interval = input.interval
        });

        return res;
    }
    [HttpGet]
    [ApiDescriptionSettings(Name = "dataTypes")]
    public async Task<SqlSugarPagedList<MqttDataPublishStatus>> MqttDataPublishStatus()
    {
        //MQTT ProcessData启动发布
        var res = await _grpcService.GetDataPublishStatus();
        SqlSugarPagedList<MqttDataPublishStatus> sqlSugarPagedList = new SqlSugarPagedList<MqttDataPublishStatus>();
        if (res != null)
        {
            sqlSugarPagedList.Total = res.DataPublishStatus.Count;
            sqlSugarPagedList.Items = res.DataPublishStatus;
        }
        sqlSugarPagedList.TotalPages = 1;
        sqlSugarPagedList.Page = 1;
        sqlSugarPagedList.HasNextPage = false;
        sqlSugarPagedList.HasPrevPage = false;
        sqlSugarPagedList.PageSize = 100;

        return sqlSugarPagedList;
    }

    [HttpGet]
    [ApiDescriptionSettings(Name = "dataType")]
    public async Task<MqttDataPublishStatus> Get([FromQuery] MqttDataPublishStatus inputt)
    {
        var res = await _grpcService.GetDataPublishStatus();
        return res.DataPublishStatus.FirstOrDefault(u => u.DataType == inputt.DataType);
    }
}
